package com.traffic.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Date;

import com.traffic.commons.utils.DateUtils;
import com.traffic.commons.utils.FtpUtil;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.traffic.commons.base.BaseController;
import com.traffic.commons.utils.PageInfo;
import com.traffic.model.TrafficConsult;
import com.traffic.service.ITrafficConsultService;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * <p>
 * 交管资讯  前端控制器
 * </p>
 * @author Hongyue Wang
 * @since 2017-04-20
 */
@Controller
@RequestMapping("/trafficConsult")
public class TrafficConsultController extends BaseController{
    
    @Autowired private ITrafficConsultService trafficConsultService;

    private static final String propertiesPath = "properties/ftp.properties";

    private static final String imgPath = File.separator + "consult";

    @GetMapping("/manager")
    public String manager() {
        return "admin/trafficConsult/trafficConsultList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TrafficConsult trafficConsult, Integer page, Integer rows, String id_sort,String order) {
        trafficConsult.setIsDelete("N");
        EntityWrapper<TrafficConsult> ew = new EntityWrapper<TrafficConsult>();
        ew.setEntity(trafficConsult);
        Page<TrafficConsult> pages = getPage(page, rows, id_sort, order);
        pages = trafficConsultService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/trafficConsult/trafficConsultAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(TrafficConsult trafficConsult, @RequestParam(value = "file") CommonsMultipartFile[] files) {
        trafficConsult.setCreatTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficConsult.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficConsult.setContent(StringEscapeUtils.unescapeHtml(trafficConsult.getContent()));
        trafficConsult.setIsDelete("N");
        String imgName = "";
        boolean b = false;
        try {
            for(int i = 0;i < files.length ; i++){
                DiskFileItem fileItem = (DiskFileItem)files[i].getFileItem();
                File file = fileItem.getStoreLocation();
                FileInputStream in = new FileInputStream(file);
                imgName = FtpUtil.getImgName();
                boolean flag = FtpUtil.uploadFile(FtpUtil.getPropertiesValue(propertiesPath, "ftp.host"),
                        Integer.valueOf(FtpUtil.getPropertiesValue(propertiesPath, "ftp.port")),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.username"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.password"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.basepath"),
                        imgPath, imgName, in);
                trafficConsult.setPicPath(imgPath + File.separator  + imgName);
                b = trafficConsultService.insert(trafficConsult);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        TrafficConsult trafficConsult = new TrafficConsult();
        trafficConsult.setId(id.intValue());
        trafficConsult.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficConsult.setIsDelete("Y");
        boolean b = trafficConsultService.updateById(trafficConsult);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TrafficConsult trafficConsult = trafficConsultService.selectById(id);
        model.addAttribute("trafficConsult", trafficConsult);
        return "admin/trafficConsult/trafficConsultEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(TrafficConsult trafficConsult, @RequestParam(value = "file") CommonsMultipartFile[] files) {
        trafficConsult.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficConsult.setContent(StringEscapeUtils.unescapeHtml(trafficConsult.getContent()));
        String imgName = "";
        boolean b = false;
        try {
            for(int i = 0;i < files.length ; i++){
                DiskFileItem fileItem = (DiskFileItem)files[i].getFileItem();
                File file = fileItem.getStoreLocation();
                FileInputStream in = new FileInputStream(file);
                imgName = FtpUtil.getImgName();
                boolean flag = FtpUtil.uploadFile(FtpUtil.getPropertiesValue(propertiesPath, "ftp.host"),
                        Integer.valueOf(FtpUtil.getPropertiesValue(propertiesPath, "ftp.port")),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.username"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.password"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.basepath"),
                        imgPath, imgName, in);
                trafficConsult.setPicPath(imgPath + File.separator  + imgName);
                b = trafficConsultService.updateById(trafficConsult);            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }
    
}
